<template>
  <view class="container">
    <uni-forms ref="form" :modelValue="form" labelWidth="120px" labelPosition="top">
      <view class="hidden-title">提交验收</view>
      <view class="background-white bor-top">
        <uni-forms-item label="总费用" name="totalCost" required>
          <uni-easyinput v-model="form.totalCost" placeholder="请输入总费用(万元)" />
        </uni-forms-item>
        <uni-forms-item label="配件费用" name="partsCost" required>
          <uni-easyinput v-model="form.partsCost" placeholder="请输入配件费用(万元)" />
        </uni-forms-item>
        <uni-forms-item label="其他费用" name="otherCost" required>
          <uni-easyinput v-model="form.otherCost" placeholder="请输入其他费用(万元)" />
        </uni-forms-item>
      </view>
      <view class="background-white bor-top bottom">
        <uni-forms-item name="deviceImageRelationEntityList" label="完工照片（最多3张）" labelWidth="150" required>
          <image-upload state="edit" :imageUrl="form.imgList" @change="imageOnChange"></image-upload>
        </uni-forms-item>
      </view>
      <view class="hidden-title">备件使用情况</view>
      <view class="background-white bor-top">
        <template v-for="(item, index) in form.partList">
          <uni-forms-item
            label="备件名称"
            required
            :rules="[{ required: true, errorMessage: '备件名称项必填' }]"
            :key="item.id"
            :name="['partList', index, 'partName']"
          >
            <view class="form-item">
              <uni-easyinput v-model="form.partList[index].partName" placeholder="请输入备件名称" />
            </view>
          </uni-forms-item>
          <uni-forms-item
            label="备件数量"
            required
            :rules="[{ required: true, errorMessage: '数量说明项必填' }]"
            :key="item.id"
            :name="['partList', index, 'partNum']"
          >
            <view class="form-item">
              <uni-easyinput v-model="form.partList[index].partNum" placeholder="请输入数量说明" />
            </view>
          </uni-forms-item>
          <uni-forms-item
            label="实际使用数量"
            required
            :rules="[{ required: true, errorMessage: '实际使用数量项必填' }]"
            :key="item.id"
            :name="['partList', index, 'partUseNum']"
          >
            <view class="form-item">
              <uni-easyinput v-model="form.partList[index].partUseNum" placeholder="请输入实际使用数量" />
            </view>
          </uni-forms-item>
        </template>
      </view>
    </uni-forms>
    <view class="submit-group">
      <button class="u-button" :ripple="true" @click="submit">提验</button>
    </view>
  </view>
</template>

<script>
import CuCellGroup from '@/components/cu-cell/cu-cell-group.vue'
import UserSelector from '@/components/user-selector/index.vue'
import { check, getPart } from '@/api/repair/repair.js'
import ImageUpload from '@/components/image-upload/index.vue'

export default {
  components: {
    CuCellGroup,
    UserSelector,
    ImageUpload
  },
  data() {
    return {
      id: '',
      procTaskId: '',
      form: {
        totalCost: '',
        partsCost: '',
        otherCost: '',
        imgList: '',
        partList: []
      },
      imageList: []
    }
  },
  onLoad(option) {
    if (!option.data) return
    let data = JSON.parse(option.data)
    this.id = data.id
    this.procTaskId = data.procTaskId
    this.getPartsList(this.id)
  },
  methods: {
    imageOnChange(url) {
      console.log('url', url)
      url.map((u) => {
        this.imageList.push({
          imagePath: u
        })
      })
    },
    async getPartsList(examineId) {
      const res = await getPart(examineId)
      res.data.map((m) => {
        this.form.partList.push({
          partId: m.id,
          partName: m.partName,
          partNum: m.partNum,
          partUseNum: m.partUseNum
        })
      })
    },
    submit() {
      this.$refs.form.validate().then((res) => {
        let data = {
          id: this.id,
          procTaskId: this.procTaskId,
          deviceExamineWorkflowEntity: {
            examineId: this.id,
            opinionType: 'PASS'
          },
          checkInfo: {
            ...this.form,
            imageList: this.imageList
          }
        }
        check(data).then((res) => {
          this.$modal.showToast('处理成功')
          uni.$emit('refresh')
          uni.navigateBack()
        })
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  margin-bottom: 200rpx;

  .background-white {
    background-color: #fff;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .uni-forms-item__label {
      color: #333;
      font-size: 30rpx;
    }

    .bor-btm {
      display: flex;
      border-bottom: 1px solid #d5d7e3;
    }

    .photo {
      display: flex;
      justify-content: space-between;

      image {
        width: 214rpx;
        height: 214rpx;
      }
    }

    .text {
      padding-bottom: 20rpx;
      padding-top: 4rpx;
      color: #333;

      text {
        color: #666;
      }
    }
  }
}

.hidden-title {
  background: #f5f5f5;
  padding: 20rpx;
  color: #999;
}

.bor-top {
  border-top: 20rpx solid #f5f5f5;
}

.blue {
  color: #88b4c4;
}

.icon-style {
  width: 52rpx;
  height: 42rpx;
  padding-right: 10rpx;
}
.input {
  display: inline-block;
  width: 100%;
  background-color: #f5f5f5;
  padding: 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  height: 60rpx;
}
</style>
